/* 业务逻辑 */
(function() {
	Path.on(["/__basename__.html", "/"], function() {
		App.set("$Cache.current_page", 1);
		//获取指定页的商品
		coAjax.get(appConfig.goods.list_random, {
			bus_id: busInfo._id,
			num: 9,
			page: 0
		}, function(result) {
			var goods_list = result.result;
			App.set("$Cache.main.goods_list", goods_list);
		}, function() {
			console.log(arguments);
		});
	});

	// 加载分类树
	coAjax.get(appConfig.bus_classify_tree, {
		bus_id: busInfo._id
	}, function(classify_tree) {
		console.log(classify_tree.result);
		classify_tree.result.childs.forEach(function(classify, index) {
			coAjax.get(appConfig.bus_classify_all_child_goods_by_id + classify.classify._id, {
				bus_id: busInfo._id
			}, function(classify_info) {
				App.concat("$Cache.main.classify_list", [{
					_id: classify.classify._id,
					title: classify.classify.classify_name,
					contents: classify_info.result
				}]);
				App.set("$Cache.main.loading_tags_completed", true);
			});
		});
	});
	// 加载标签列表
	// coAjax.get(appConfig.bus.tags_detail, {
	// 	bus_id: busInfo._id
	// }, function(result) {
	// 	console.log(result.result);
	// 	result.result.forEach(function(tag, index) {
	// 		App.concat("$Cache.main.tags", [{
	// 			_id: tag._id,
	// 			tag_name: tag.tag_name,
	// 			detail_content: tag.detail_content.reverse() //倒序排列商品
	// 		}]);
	// 		App.set("$Cache.main.loading_tags_completed", true);
	// 	});
	// 	// App.set("$Cache.main.tags", result.result);
	// });

	// 加载店铺列表
	var remote_server_url = "http://121.40.18.23:1518/api/complex/ClientStoreMapFactory";
	// var remote_server_url = "http://112.74.213.5:1518/api/complex/ClientStoreMapFactory";
	// var remote_server_url = "http://localhost:1518/api/complex/ClientStoreMapFactory";
	var dtd = new $.Deferred();
	var _get_store_list = function(dtd) {
		$.getJSON(remote_server_url, {
			page_size: 20
		}, function(store_list_result) {
			console.log(store_list_result)
			console.log("store_list_result")
			App.set("$Cache.main.store_list", store_list_result.contents);
			store_list = store_list_result.contents;
			dtd.resolve(store_list_result.contents);
		});
		return dtd;
	};
	$.when(_get_store_list(dtd)).then(function(stores) {
		// 获取小店轮播器
		var store_list = $(".store_list")[0];
		var $store_list = $(store_list);
		var $store_list_scrollWidth = store_list.scrollWidth;
		if ($store_list_scrollWidth) {
			var $width = $store_list.width();
			var _init = (~~($store_list_scrollWidth / $width)) + 1;
			var _i = 0;
			setInterval(function() {
				// 小店轮播器
				$store_list.stop().animate({
					scrollLeft: $width * (_i % _init)
				}, 300);
				_i++;
			}, 5000);
		};
	});

	// 搜索
	App.set('$Event.href', function() {
		if (App.get('$Cache.search_text')) {
			Path.jump("searchset.html?q=" + App.get('$Cache.search_text'))
		} else {
			alert('error', '请输入搜索关键词');
		};
	});
	// 城市按钮
	App.set("$Event.main.citymenu", function() {
		App.model.toggle("$Cache.main.citymenu");
	});
	// 更换城市
	App.set("$Event.main.changecity", function() {
		myConfirm("暂未开放");
	});
	// 报告城市错误
	App.set("$Event.main.cityerror", function() {
		myConfirm("错误信息已提交，感谢您的反馈");
	});
}());

/*交互*/
;